package com.medicine.hbp.ui;

import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.medicine.hbp.R;
import com.medicine.hbp.adapter.MyMoneyAdapter;
import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.findWalletByUserObjectVo;
import com.medicine.hbp.mob.findWalletByUserVo;
import com.medicine.hbp.mob.findlistbyuseridListVo;
import com.medicine.hbp.uitl.ToastUtil;

public class MyMoneyActivity extends BaseActivity {
	@BindView(id = R.id.xListView, click = false)
	public ListView listView;

	@BindView(id = R.id.btn_tx, click = true)
	public Button btn_tx;

	@BindView(id = R.id.txt_synum)
	public TextView txt_synum;//剩余收到的感谢金额
	@BindView(id = R.id.txt_zjnum)
	public TextView txt_zjnum;//总共收到的感谢金额

	private MyMoneyAdapter adapter;

	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_my_money);
	}

	@Override
	public void initWidget() {
		super.initWidget();
		adapter = new MyMoneyAdapter(aty);
		listView.setAdapter(adapter);
//		adapter.setItems(list);
//		adapter.notifyDataSetChanged();
		txt_zjnum.setText("您总计收到感谢金额为：***元");
		initTop();
		httpfindlistbyuserid();
		httpfindUserPayInfo();
	}
	private void initTop() {
		TopView topview = (TopView) findViewById(R.id.topview);
		topview.setTitle("我的钱包");
		topview.setLeftVisibility(View.GONE);
		topview.setLeftVisibilityExit(View.VISIBLE);
		topview.setRightVisibility(View.INVISIBLE);
		topview.setLeftOnClickExit(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
	}
	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
			case R.id.btn_tx:
				try {
					if(info.walletStatus.equals("Y")){
						ToastUtil.showShort(aty, "正在提现中");
						return;
					}
					if(info.walletPrice<100){
						ToastUtil.showShort(aty, "金额大于100元才可以提现哦！");
						return;
					}
					Intent intent =new Intent(aty ,WithdrawDepositActivity.class);
					startActivity(intent);
				} catch (Exception e) {
					ToastUtil.showShort(aty, "金额不足");
				}

				break;

			default:
				break;
		}
	}
	private findWalletByUserVo info;
	@Override
	protected void onResume() {
		super.onResume();
		http();
	}
	/**
	 * 获取钱包信息接口
	 */
	private void http() {
		HttpParams params = new HttpParams();
		AppHttps.HttpPost(aty, HttpInterface.FIND_WALLET_BY_USERID, params,
				new AppHttpCallBack<findWalletByUserObjectVo>(findWalletByUserObjectVo.class, aty) {
					@Override
					public void onSuccess(findWalletByUserObjectVo result) {
						try {
							info=result.info;
							txt_synum.setText(info.walletPrice+"");
							txt_zjnum.setText("您总计收到感谢金额为："+info.walletSumPrice+"元");
							if(info.walletStatus.equals("Y")){
								btn_tx.setText("正在提现中");
							}
						} catch (Exception e) {
							txt_synum.setText("0");
							txt_zjnum.setText("您总计收到感谢金额为：0元");
						}

					}
					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});

	}

	/**
	 * 用户自己的打赏
	 */
	private void httpfindlistbyuserid() {
		HttpParams params = new HttpParams();
		AppHttps.HttpPost(aty, HttpInterface.FIND_LIST_BY_USERID, params,
				new AppHttpCallBack<findlistbyuseridListVo>(findlistbyuseridListVo.class, aty) {
					@Override
					public void onSuccess(findlistbyuseridListVo result) {
						if(result.rewardList!=null){
							adapter.setItems(result.rewardList);
							adapter.notifyDataSetChanged();
						}
					}
					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});
	}


	/**
	 * 获取用户支付宝账户信息接口
	 */
	private void httpfindUserPayInfo() {
		HttpParams params = new HttpParams();
		AppHttps.HttpPost(aty, HttpInterface.FIND_USER_PAY_INFO, params,
				new AppHttpCallBack<findlistbyuseridListVo>(findlistbyuseridListVo.class, aty) {
					@Override
					public void onSuccess(findlistbyuseridListVo result) {
					}
					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});
	}

}
